﻿/* 
 * Copyright (c) 2010 Letwory Interactive
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -- Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * -- Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * Original contributor: Nathan Letwory
 */

using System;

namespace VerseSharp
{
    public struct MaterialRampPoint
    {
        public double Position;
        public double Red;
        public double Green;
        public double Blue;
    }

    public class MaterialFragmentRamp: IMaterialFragment
    {
        public FragmentType ID { get { return FragmentType.Ramp; } }
        public MaterialRampType Type;
        public MaterialRampChannel Channel;
        public ushort Mapping;
        public uint PointCount; // note, serialised as byte
        public ushort DataControl;
        public MaterialRampPoint[] Ramp;

        public void Deserialize(byte[] source, ref int pos)
        {
            Type = (MaterialRampType)source[pos++];
            Channel = (MaterialRampChannel)source[pos++];
            Mapping = Utils.GetUInt16(source, ref pos);
            PointCount = (uint)source[pos++];
            if (PointCount > 0)
            {
                Ramp = new MaterialRampPoint[PointCount];
            }
            for (int i = 0; i < PointCount; i++)
            {
                Ramp[i].Position = Utils.GetReal64(source, ref pos);
                Ramp[i].Red = Utils.GetReal64(source, ref pos);
                Ramp[i].Green = Utils.GetReal64(source, ref pos);
                Ramp[i].Blue = Utils.GetReal64(source, ref pos);
            }
        }

        public void Serialize(byte[] destination, ref int pos)
        {
            destination[pos++] = (byte)Type;
            destination[pos++] = (byte)Channel;
            Utils.PutUInt16(destination, ref pos, Mapping);
            destination[pos++] = (byte)PointCount;
            for (int i = 0; i < PointCount; i++)
            {
                Utils.PutReal64(destination, ref pos, Ramp[i].Position);
                Utils.PutReal64(destination, ref pos, Ramp[i].Red);
                Utils.PutReal64(destination, ref pos, Ramp[i].Green);
                Utils.PutReal64(destination, ref pos, Ramp[i].Blue);
            }
        }
    }
}
